package cs236369.webserver.queue.handlers;

import java.util.concurrent.ConcurrentLinkedQueue;

public abstract class Worker<T> extends Thread {
	ConcurrentLinkedQueue<T> taskQueue = null;

	public Worker(ConcurrentLinkedQueue<T> queue) {
		taskQueue = queue;
	}

	public void run() {
		T ObjectToHandle = null;
		while (true) {
			synchronized (taskQueue) {
				while (taskQueue.isEmpty()) {
					try {
						taskQueue.wait();
					} catch (InterruptedException ignored) {
					}
				}
				ObjectToHandle = taskQueue.poll();
			}
			handleTask(ObjectToHandle);
		}
	}

	protected abstract void handleTask(T objectToHandle);
}
